/**
 ********************************************************************************
 *
 *文件名称：
 *
 *文件日期：
 *
 *文件描述：
 *
 *文件版本：
 *
 *修改历史：
 *
 ********************************************************************************
 ***/

/*******************************	头文件包含区	**********************************/
#include "protocol.h"
#include "at_cmd_tx.h"
#include "module_control.h"
#include "uart.h"
#include "sensor.h"
/******************************* 全局变量定义区 **********************************/
protocolSize protocol_size = {0};
protocolStruct protocol = {0};
compare_Struct compareStruct;
/*如果分配到的地址为奇数，则在强制转换成uint16_t*时，操作地址会进入硬件中断，因为地址不合法,故将此结构体定义在指定位置*/
regStruct reg __attribute__((at(0x10000100)));

R_ALARM_struct ALARM_R __attribute__((at(0x100016D6)));;

uint16_t flash_data[FLASH_DATA_BUF];

/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/
void  param_init(void)
{
	LOGD("para init\r\n");
	protocol_size.rw = sizeof(reg.RW);
	protocol_size.w = sizeof(reg.W);
	protocol_size.r = sizeof(reg.R);
	memset(&reg, 0, sizeof(reg));
	reg.RW.year_mon = 0;
	reg.RW.day_hour = 0;
	reg.RW.min_sec = 0;
	reg.RW.baud = 0;
	reg.RW.ota_start_flag = 0;
	reg.RW.ota_flag = 0;
	reg.RW.lamp[0].light = 100;
	reg.RW.lamp[0].sw = 1;

	reg.RW.device_type = 0; 
	reg.W.v_alarm_set = 290;
	reg.W.i_alarm_set = 2800;
	reg.W.temp_alarm_set = 800;
	
	reg.W.low_temp_alarm_set = -300;
	ALARM_R.v_alarm_least = 0xFFFF;
	ALARM_R.low_temp_alarm_max = 0xFFFF;

	reg.W.data_tx_time = 3600;
	reg.W.heartbeat_interval = 1800;
	reg.W.lowU_set = 100;
	reg.W.power_set = 300;
	reg.W.restart = 0;
	reg.W.all_param_clear = 0;
	reg.W.module_time_clear = 0;
	reg.W.lamp_time_clear = 0;
	reg.W.ota_sum_len = 0;
	reg.W.software_len = 0;
	reg.W.scene_clear = 0;
	reg.W.lamp_cpmpare_xy = 0;
	reg.W.lamp_cpmpare_z = 0x0020;
	reg.W.leak_current_alarm_set = 30;
	reg.W.longitude_h = 0x0073;
	reg.W.longitude_l = 0x2000;
	reg.W.dimensionality_h = 0x0016;
	reg.W.dimensionality_l = 0x2000;
	reg.R.module_run_time = 0;
	reg.R.lamp1_run_time = 0;
	reg.R.lamp2_run_time = 0;
	reg.R.v = 2200;
	reg.RW.moso_max_light = 65;
	reg.W.moso_alarm_pwm = 60;
}

/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/
void restart_init(void)
{
	uint8_t i = 0;
	reg.R.module_run_time = 0;
	reg.R.lamp1_run_time = 0;
	reg.R.lamp2_run_time = 0;
	if (!reg.R.run_state)
	{
	#ifdef LAMP_CHANGE
		reg.RW.lamp[0].sw = 1;
		reg.RW.lamp[0].light = 10;
	#else
		reg.RW.lamp[0].sw = 1;
		reg.RW.lamp[0].light = 100;
	#endif
	}
	device_time.run_data_report_time = 0;
	protocol_size.rw = sizeof(reg.RW);
	protocol_size.w = sizeof(reg.W);
	protocol_size.r = sizeof(reg.R);

	for (i = 0; i < FUN_POINT_BUF_SIZE; i++)
	{
		my_tx_fun[i] = send_waiting_to_receive;
		my_rx_fun[i] = recv_waiting_to_receive;
	}
}
/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/
void fun_code_read(void)
{
	volatile uint16_t i, data, data_len = 0, crc;
#if (PROJECT_TYPEDEF == NB)
	main_tx.my_at_sendbuf[0] = 0x02; // 上报02
	main_tx.my_at_sendbuf[1] = 0x00; //
	main_tx.my_at_sendbuf[2] = 0x04; //服务ID为04
	main_tx.my_at_sendbuf[3] = 0x00; //
	data_len = 7;
#endif
#ifdef PUBLIC_PROTOCOL
	main_tx.my_at_sendbuf[data_len++] = reg.R.m_73or63 ? 0x63 : 0x73;
#else
	main_tx.my_at_sendbuf[data_len++] = 0x63;
#endif
	main_tx.my_at_sendbuf[data_len++] = 0x00;
	main_tx.my_at_sendbuf[data_len++] = 0x00;
	main_tx.my_at_sendbuf[data_len++] = 0x00;
	main_tx.my_at_sendbuf[data_len++] = 0x00;
#ifdef PUBLIC_PROTOCOL
	main_tx.my_at_sendbuf[data_len++] = reg.R.m_73or63 ? 0x03 : 0x31;
#else
	main_tx.my_at_sendbuf[data_len++] = 0x03;
#endif
	main_tx.my_at_sendbuf[data_len++] = global_control.protocol.reg_addr[0] >> 8;
	main_tx.my_at_sendbuf[data_len++] = global_control.protocol.reg_addr[0];
	main_tx.my_at_sendbuf[data_len++] = global_control.protocol.reg_len[0];
	for (i = 0; i < global_control.protocol.reg_len[0]; i++)
	{
		data = protocol_read(global_control.protocol.reg_addr[0] + i);
		main_tx.my_at_sendbuf[data_len++] = data >> 8;
		main_tx.my_at_sendbuf[data_len++] = data;
	}

#if (PROJECT_TYPEDEF == NB)
	crc = crc16(main_tx.my_at_sendbuf + 7, data_len - 7);
#elif (PROJECT_TYPEDEF == NB_ONENET)
	crc = crc16(main_tx.my_at_sendbuf, data_len);
#elif (PROJECT_TYPEDEF == CAT1)
	crc = crc16(main_tx.my_at_sendbuf, data_len);
#endif
	main_tx.my_at_sendbuf[data_len++] = crc & 0x00ff;
	main_tx.my_at_sendbuf[data_len++] = crc >> 8;

#if (PROJECT_TYPEDEF == NB)
	main_tx.my_at_sendbuf[3] = (data_len - 5) >> 8; //
	main_tx.my_at_sendbuf[4] = data_len - 5;		//长度为COM_SendLen
	main_tx.my_at_sendbuf[5] = (data_len - 7) >> 8; //
	main_tx.my_at_sendbuf[6] = data_len - 7;		//长度为COM_SendLen
#endif
	myitoa(main_tx.my_at_sendbuf, data_len, main_tx.my_at_data_str);
#if (PROJECT_TYPEDEF == NB)
	sprintf((char *)main_tx.my_at_sendbuf, "AT+QLWULDATA=%d,%s\r\n", data_len, main_tx.my_at_data_str);
#elif (PROJECT_TYPEDEF == NB_ONENET)
	sprintf((char *)main_tx.my_at_sendbuf, "AT+MIPLNOTIFY=0,0,10250,0,0,2,%d,%s,0,0\r\n", data_len, main_tx.my_at_data_str);
#elif (PROJECT_TYPEDEF == CAT1)
#if (IP_PUBLIC == ELSE_IP || IP_PUBLIC == WUHAN_PROTOCOL)
	sprintf((char *)main_tx.my_at_sendbuf, "AT+QMTPUB=0,0,0,0,\"/UpLink/%s\",{\"data\":\"%s\"}\r\n", device_info.imei, main_tx.my_at_data_str);
#else
	//sprintf((char *)main_tx.my_at_sendbuf, "AT+QMTPUB=0,0,0,0,\"UpLink\",{\"data\":\"%s\"}\r\n", main_tx.my_at_data_str);
	sprintf((char *)main_tx.my_at_sendbuf, "{\"data\":\"%s\"}\r\n", main_tx.my_at_data_str);
#endif
#endif
}

/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/
void fun_code_write(void)
{
	volatile uint16_t i, j, data_len = 0, crc, data;
#if (PROJECT_TYPEDEF == NB)
	main_tx.my_at_sendbuf[0] = 0x02; // 上报02
	main_tx.my_at_sendbuf[1] = 0x00; //
	main_tx.my_at_sendbuf[2] = 0x04; //服务ID为04
	main_tx.my_at_sendbuf[3] = 0x00; //
	data_len = 7;
#endif
#ifdef PUBLIC_PROTOCOL
	main_tx.my_at_sendbuf[data_len++] = reg.R.m_73or63 ? 0x63 : 0x73;
#else
	main_tx.my_at_sendbuf[data_len++] = 0x63;
#endif
	main_tx.my_at_sendbuf[data_len++] = 0x00;
	main_tx.my_at_sendbuf[data_len++] = 0x00;
	main_tx.my_at_sendbuf[data_len++] = 0x00;
	main_tx.my_at_sendbuf[data_len++] = global_control.protocol.subpackage_cnt++;
#ifdef PUBLIC_PROTOCOL
	main_tx.my_at_sendbuf[data_len++] = reg.R.m_73or63 ? 0x10 : 0x11;
#else
	main_tx.my_at_sendbuf[data_len++] = 0x10;
#endif
	main_tx.my_at_sendbuf[data_len++] = global_control.protocol.reg_num;
	for (i = 0; i < global_control.protocol.reg_num; i++)
	{
		if (global_control.protocol.reg_addr[i] == 0x201E)
			global_control.protocol.reg_len[i] = 1; //特殊处理，策略包恢复数据长度为1
		main_tx.my_at_sendbuf[data_len++] = global_control.protocol.reg_addr[i] >> 8;
		main_tx.my_at_sendbuf[data_len++] = global_control.protocol.reg_addr[i];
		main_tx.my_at_sendbuf[data_len++] = global_control.protocol.reg_len[i];

		if (!(global_control.protocol.reg_addr[i] >= 0x2700 && global_control.protocol.reg_addr[i] <= 0x3000)) //特殊处理，升级包不回复数据
		{
			for (j = 0; j < global_control.protocol.reg_len[i]; j++)
			{
				data = protocol_read(global_control.protocol.reg_addr[i] + j);
				main_tx.my_at_sendbuf[data_len++] = data >> 8;
				main_tx.my_at_sendbuf[data_len++] = data;
			}
		}
	}
#if (PROJECT_TYPEDEF == NB)
	crc = crc16(main_tx.my_at_sendbuf + 7, data_len - 7);
#elif (PROJECT_TYPEDEF == NB_ONENET)
	crc = crc16(main_tx.my_at_sendbuf, data_len);
#elif (PROJECT_TYPEDEF == CAT1)
	crc = crc16(main_tx.my_at_sendbuf, data_len);
#endif
	main_tx.my_at_sendbuf[data_len++] = crc & 0x00ff;
	main_tx.my_at_sendbuf[data_len++] = crc >> 8;

#if (PROJECT_TYPEDEF == NB)
	main_tx.my_at_sendbuf[3] = (data_len - 5) >> 8; //
	main_tx.my_at_sendbuf[4] = data_len - 5;		//长度为COM_SendLen
	main_tx.my_at_sendbuf[5] = (data_len - 7) >> 8; //
	main_tx.my_at_sendbuf[6] = data_len - 7;		//长度为COM_SendLen
#endif
	myitoa(main_tx.my_at_sendbuf, data_len, main_tx.my_at_data_str);
#if (PROJECT_TYPEDEF == NB)
	sprintf((char *)main_tx.my_at_sendbuf, "AT+QLWULDATA=%d,%s\r\n", data_len, main_tx.my_at_data_str);
#elif (PROJECT_TYPEDEF == NB_ONENET)
	sprintf((char *)main_tx.my_at_sendbuf, "AT+MIPLNOTIFY=0,0,10250,0,0,2,%d,%s,0,0\r\n", data_len, main_tx.my_at_data_str);
#elif (PROJECT_TYPEDEF == CAT1)
#if (IP_PUBLIC == ELSE_IP || IP_PUBLIC == WUHAN_PROTOCOL)
	sprintf((char *)main_tx.my_at_sendbuf, "AT+QMTPUB=0,0,0,0,\"/UpLink/%s\",{\"data\":\"%s\"}\r\n", device_info.imei, main_tx.my_at_data_str);
#else
	//sprintf((char *)main_tx.my_at_sendbuf, "AT+QMTPUB=0,0,0,0,\"UpLink\",{\"data\":\"%s\"}\r\n", main_tx.my_at_data_str);
	sprintf((char *)main_tx.my_at_sendbuf, "{\"data\":\"%s\"}\r\n", main_tx.my_at_data_str);
#endif
#endif
}

/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/
void fun_code_singleWrite(void)
{
	volatile uint16_t i, data_len = 0, crc;
#if (PROJECT_TYPEDEF == NB)
	main_tx.my_at_sendbuf[0] = 0x02; // 上报02
	main_tx.my_at_sendbuf[1] = 0x00; //
	main_tx.my_at_sendbuf[2] = 0x04; //服务ID为04
	main_tx.my_at_sendbuf[3] = 0x00; //
	data_len = 7;
#endif
#ifdef PUBLIC_PROTOCOL
	main_tx.my_at_sendbuf[data_len++] = reg.R.m_73or63 ? 0x63 : 0x73;
#else
	main_tx.my_at_sendbuf[data_len++] = 0x63;
#endif
	main_tx.my_at_sendbuf[data_len++] = 0x00;
	main_tx.my_at_sendbuf[data_len++] = 0x00;
	main_tx.my_at_sendbuf[data_len++] = 0x00;
	main_tx.my_at_sendbuf[data_len++] = 0x12;
#ifdef PUBLIC_PROTOCOL
	main_tx.my_at_sendbuf[data_len++] = reg.R.m_73or63 ? 0x06 : 0x61;
#else
	main_tx.my_at_sendbuf[data_len++] = 0x06;  
#endif
	main_tx.my_at_sendbuf[data_len++] = global_control.protocol.reg_addr[0] >> 8;
	main_tx.my_at_sendbuf[data_len++] = global_control.protocol.reg_addr[0];
	if (global_control.protocol.reg_addr[0] == 0x200C ||
		(global_control.protocol.reg_addr[0] >= 0x2700 && global_control.protocol.reg_addr[0] < 0x3000))
	{
		//在升级的时候，如果发固件包长度这个寄存器，回包的时候要回0x1007的内容
		main_tx.my_at_sendbuf[data_len++] = reg.RW.ota_start_flag >> 8;
		main_tx.my_at_sendbuf[data_len++] = reg.RW.ota_start_flag;
	}
	else if (global_control.protocol.reg_addr[0] == 0x1008)
	{
		if (reg.RW.ota_flag == OTA_FAIL)
		{
			main_tx.my_at_sendbuf[data_len++] = global_control.protocol.data[0] >> 8;
			main_tx.my_at_sendbuf[data_len++] = global_control.protocol.data[0];
			main_tx.my_at_sendbuf[data_len++] = reg.RW.ota_start_flag >> 8;
			main_tx.my_at_sendbuf[data_len++] = reg.RW.ota_start_flag;
		}
		else if (reg.RW.ota_flag == OTA_START)
		{
			main_tx.my_at_sendbuf[data_len++] = reg.RW.ota_start_flag >> 8;
			main_tx.my_at_sendbuf[data_len++] = reg.RW.ota_start_flag;
		}
		else
		{
			main_tx.my_at_sendbuf[data_len++] = global_control.protocol.data[0] >> 8;
			main_tx.my_at_sendbuf[data_len++] = global_control.protocol.data[0];
		}
	}
	else
	{
		main_tx.my_at_sendbuf[data_len++] = global_control.protocol.data[0] >> 8;
		main_tx.my_at_sendbuf[data_len++] = global_control.protocol.data[0];
	}
#if (PROJECT_TYPEDEF == NB)
	crc = crc16(main_tx.my_at_sendbuf + 7, data_len - 7);
#elif (PROJECT_TYPEDEF == NB_ONENET)
	crc = crc16(main_tx.my_at_sendbuf, data_len);
#elif (PROJECT_TYPEDEF == CAT1)
	crc = crc16(main_tx.my_at_sendbuf, data_len);
#endif
	main_tx.my_at_sendbuf[data_len++] = crc & 0x00ff;
	main_tx.my_at_sendbuf[data_len++] = crc >> 8;
#if (PROJECT_TYPEDEF == NB)
	main_tx.my_at_sendbuf[3] = (data_len - 5) >> 8; //
	main_tx.my_at_sendbuf[4] = data_len - 5;		//长度为COM_SendLen
	main_tx.my_at_sendbuf[5] = (data_len - 7) >> 8; //
	main_tx.my_at_sendbuf[6] = data_len - 7;		//长度为COM_SendLen
#endif
	myitoa(main_tx.my_at_sendbuf, data_len, main_tx.my_at_data_str);
#if (PROJECT_TYPEDEF == NB)
	sprintf((char *)main_tx.my_at_sendbuf, "AT+QLWULDATA=%d,%s\r\n", data_len, main_tx.my_at_data_str);
#elif (PROJECT_TYPEDEF == NB_ONENET)
	sprintf((char *)main_tx.my_at_sendbuf, "AT+MIPLNOTIFY=0,0,10250,0,0,2,%d,%s,0,0\r\n", data_len, main_tx.my_at_data_str);
#elif (PROJECT_TYPEDEF == CAT1)
#if (IP_PUBLIC == ELSE_IP || IP_PUBLIC == WUHAN_PROTOCOL)
	sprintf((char *)main_tx.my_at_sendbuf, "AT+QMTPUB=0,0,0,0,\"/UpLink/%s\",{\"data\":\"%s\"}\r\n", device_info.imei, main_tx.my_at_data_str);
#else
	//sprintf((char *)main_tx.my_at_sendbuf, "AT+QMTPUB=0,0,0,0,\"UpLink\",{\"data\":\"%s\"}\r\n", main_tx.my_at_data_str);
	sprintf((char *)main_tx.my_at_sendbuf, "{\"data\":\"%s\"}\r\n", main_tx.my_at_data_str);
#endif
#endif
}
/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/
uint16_t swapUint16_t(uint16_t data)
{
	volatile uint16_t new_data;
	volatile uint8_t data_h, data_l;

	data_h = data >> 8;
	data_l = data & 0x00ff;
	new_data = ((uint16_t)data_l << 8) + data_h;

	return new_data;
}

void swapUint16_t_Plus(uint16_t *indata, uint16_t len)
{
	volatile uint16_t new_data;
	volatile uint8_t data_h, data_l;

	for (int i = 0; i < len; i++)
	{
		data_h = *(indata + i) >> 8;
		data_l = *(indata + i) & 0x00ff;
		*(indata + i) = ((uint16_t)data_l << 8) + data_h;
	}
}

/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/
uint32_t swapUint32_t(uint32_t data)
{
	volatile uint32_t new_data;
	volatile uint16_t data_h, data_l;

	data_h = data >> 16;
	data_l = data & 0x00ffff;
	new_data = ((uint16_t)data_l << 16) + data_h;

	return new_data;
}
/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/

uint16_t pCharToU16(uint8_t *in_p)
{
	uint16_t return_data;
	uint8_t data_h, data_l;
	uint8_t *p = in_p;

	data_h = *p++;
	data_l = *p;
	return_data = (uint16_t)(data_h << 8) + data_l;
	return return_data;
}
/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/
static void calculate_Latitude_longitude(uint8_t i)
{
	volatile uint16_t hourMin;
	//uint32_t run_time;
	//static uint8_t flag = 0;
	/*	如果在策略执行期间，使用当天的年月日*/
	switch (reg.W.scene[i].scene_type)
	{
	/*按时*/
	case 1:
		timestampTable[i] = TimeDate_To_TimeStamp(
			rtc_time.year,
			rtc_time.month,
			rtc_time.date,
			reg.W.scene[i].hour,
			reg.W.scene[i].min,
			reg.W.scene[i].sec);
		break;

	/*日出之前*/
	case 2:
		hourMin = (reg.R.sunset >> 8) * 60 + (reg.R.sunset & 0x00ff);
		hourMin = hourMin - (reg.W.scene[i].hour * 60 + reg.W.scene[i].min);

		timestampTable[i] = TimeDate_To_TimeStamp(
			rtc_time.year,
			rtc_time.month,
			rtc_time.date,
			hourMin / 60,
			hourMin % 60,
			0);
		break;

	/*日出之后*/
	case 3:
		hourMin = (reg.R.sunset >> 8) * 60 + (reg.R.sunset & 0x00ff);
		hourMin = hourMin + (reg.W.scene[i].hour * 60 + reg.W.scene[i].min);

		timestampTable[i] = TimeDate_To_TimeStamp(
			rtc_time.year,
			rtc_time.month,
			rtc_time.date,
			hourMin / 60,
			hourMin % 60,
			0);
		break;

	/*日落之前*/
	case 4:
		hourMin = (reg.R.sunrise >> 8) * 60 + (reg.R.sunrise & 0x00ff);
		hourMin = hourMin - (reg.W.scene[i].hour * 60 + reg.W.scene[i].min);
		timestampTable[i] = TimeDate_To_TimeStamp(
			rtc_time.year,
			rtc_time.month,
			rtc_time.date,
			hourMin / 60,
			hourMin % 60,
			0);
		break;

	/*日落之后*/
	case 5:
		hourMin = (reg.R.sunrise >> 8) * 60 + (reg.R.sunrise & 0x00ff);
		hourMin = hourMin + (reg.W.scene[i].hour * 60 + reg.W.scene[i].min);

		timestampTable[i] = TimeDate_To_TimeStamp(
			rtc_time.year,
			rtc_time.month,
			rtc_time.date,
			hourMin / 60,
			hourMin % 60,
			0);
		break;
		/*按上电时间*/
	case 6:
		timestampTable[i] = TimeDate_To_TimeStamp(
		(reg.R.powerOn_year_mon >> 8),
		reg.R.powerOn_year_mon,
		reg.R.powerOn_day_hour >> 8,
		reg.R.powerOn_day_hour,
		reg.R.powerOn_min_sec >> 8,
		reg.R.powerOn_min_sec)  + reg.W.scene[i].hour * 60 * 60 + reg.W.scene[i].min * 60 + reg.W.scene[i].sec;
		break;
	default:
		break;
	}
}
uint8_t FirstSort = 0;
void sort(uint32_t *timeStampBuf, scene_struct *sceneBuf, uint8_t len)
{
	uint8_t i, j;
	//static uint8_t flag = 0;
	uint32_t run_time;
	scene_struct scene;
	volatile uint32_t t, timeStamp, start_timestamp, end_timestamp, timeLastStamp = 0;

	timeStamp = TimeDate_To_TimeStamp(rtc_time.year, rtc_time.month, rtc_time.date, rtc_time.hour, rtc_time.min, rtc_time.sec);
	for (i = 0; i < len; i++)
	{
		start_timestamp = TimeDate_To_TimeStamp(reg.W.scene[i].start_year,
												reg.W.scene[i].start_mon,
												reg.W.scene[i].start_date,
												0,
												0,
												0);
		end_timestamp = TimeDate_To_TimeStamp(reg.W.scene[i].end_year,
											  reg.W.scene[i].end_mon,
											  reg.W.scene[i].end_date,
											  23,
											  59,
											  59);
		if (start_timestamp == 0)
		{
			/*	如果无策略	*/
			timestampTable[i] = 0;
		}
		else if (!((reg.W.scene[i].time_type) & (0x01 << rtc_time.iWeek)))
		{
			timestampTable[i] = 4070880001;
		}
		else if(rtc_time.year < 21 || rtc_time.year >= 60)
		{
			/*未获取到网络时间，上电执行动作参与排序，其余动作不参与排序*/
			if(sceneBuf[i].scene_type == 0x06)
			{
				if(FirstSort < 2)
				{
					run_time = (reg.R.module_run_time >> 16 | reg.R.module_run_time << 16);
					timeStampBuf[i] = TimeDate_To_TimeStamp(
					rtc_time.year,
					rtc_time.month,
					rtc_time.date,
					rtc_time.hour,
					rtc_time.min,
					0) - run_time * 60 + sceneBuf[i].hour * 60 * 60 + sceneBuf[i].min * 60 + sceneBuf[i].sec;

					if(timeStamp >= timeStampBuf[i])
					{
						timeStampBuf[i] = timeStamp + 1;
					}
				}				
			}
			else{
				timeStampBuf[i] = 4070880001;
			}
		}
		else if (timeStamp < start_timestamp)
		{
			// 当前时间小于开始时间，动作未到开始日期
			timestampTable[i] = 4070880001;
		}
		else if (timeStamp >= start_timestamp && timeStamp <= end_timestamp)
		{
			calculate_Latitude_longitude(i);
		}
		else
		{
			/*	过期的策略将其设置到一个永远无法到达的日期2099-01-01 00:00:01	*/
			timestampTable[i] = 4070880001;
		}
	}
	if(FirstSort < 2)
		FirstSort ++;
	//排序
	for (i = 0; i < len - 1; i++)
	{
		for (j = 0; j < len - i - 1; j++)
		{
			if (timeStampBuf[j] > timeStampBuf[j + 1])
			{
				t = timeStampBuf[j];
				timeStampBuf[j] = timeStampBuf[j + 1];
				timeStampBuf[j + 1] = t;

				scene = sceneBuf[j];
				sceneBuf[j] = sceneBuf[j + 1];
				sceneBuf[j + 1] = scene;
			}
		}
	}
	//刷新过去时间和未来时间
	memset(&compareStruct, 0, sizeof(compareStruct));
	for (i = 0; i < SCENE_TIME_NUM; i++)
	{
		if (timeStamp >= timestampTable[i])
		{
			compareStruct.lastScene = reg.W.scene[i];
			compareStruct.lastSceneSeek = i;
		}
		else
		{
			compareStruct.nextScene = reg.W.scene[i];
			compareStruct.nextSceneSeek = i;
			break;
		}
	}

	if (compareStruct.lastScene.start_year == 0 && compareStruct.lastScene.end_year == 0 && compareStruct.nextScene.start_date != 0)
	{
		/*如果已执行的策略为空，就从时间戳中查找最后一次执行过的未过期的策略*/
		for (i = 0; i < SCENE_TIME_NUM; i++)
		{
			if (timestampTable[7 - i] != 4070880001)
			{
				timeLastStamp = TimeDate_To_TimeStamp(reg.W.scene[7 - i].start_year, reg.W.scene[7 - i].start_mon, reg.W.scene[7 - i].start_date,
													  rtc_time.hour, rtc_time.min, rtc_time.sec);
				if (timeStamp > timeLastStamp)
				{
					compareStruct.lastScene = reg.W.scene[7 - i];
					compareStruct.lastSceneSeek = 7 - i;
					break;
				}
				else
				{
					/*do nothing*/
				}
			}
			else
			{
				/*do nothing*/
			}
		}
	}
}

/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/
extern u8 led1RestartFlag; //刚上电时触发渐变开关灯效果标志
#if (PROJECT_TYPEDEF == NB_ONENET)
bool sceneReceiveFinishFlag = TRUE;
#endif
void scene_check(void)
{
	volatile uint32_t i = 0, timeStamp = 0,timeStampMorning = 0;
	volatile static uint8_t flag = 0;

	timeStamp = TimeDate_To_TimeStamp(rtc_time.year, rtc_time.month, rtc_time.date, rtc_time.hour, rtc_time.min, rtc_time.sec);
	timeStampMorning = TimeDate_To_TimeStamp(rtc_time.year, rtc_time.month, rtc_time.date, 0, 0, 0);
	leakDete.system_run_timestamp = timeStamp;

	if (led1RestartFlag != 2 || reg.W.flash_data)
	{
		return;
	}
//	timeStamp = TimeDate_To_TimeStamp(rtc_time.year, rtc_time.month, rtc_time.date, rtc_time.hour, rtc_time.min, rtc_time.sec);
//	timeStampMorning = TimeDate_To_TimeStamp(rtc_time.year, rtc_time.month, rtc_time.date, 0, 0, 0);
//	leakDete.system_run_timestamp = timeStamp;
#if (PROJECT_TYPEDEF == NB_ONENET)
	if (timeStamp >= timestampTable[compareStruct.nextSceneSeek] && sceneReceiveFinishFlag) //达到下一个时间表，更新策略
#else
	if (timeStamp >= timestampTable[compareStruct.nextSceneSeek]) //达到下一个时间表，更新策略
#endif
	{
		if (flag)
			return;
			
		if (!(rtc_time.hour == 0 && rtc_time.min == 0 && rtc_time.sec == 0))
			sort(timestampTable, reg.W.scene, SCENE_TIME_NUM); //根据时间戳对时间戳和策略时间表重新排序
										   //只判断一次
		// //根据上个时间策略内容执行开关灯
		for (i = 0; i < compareStruct.lastScene.time_ctrl_ch_num; i++)
		{
			if(flag == 1)
			{
				if(timeStampMorning <= timestampTable[compareStruct.lastSceneSeek])
				{
					reg.RW.lamp[i] = compareStruct.lastScene.ctrl_buf[i];
				}
				else
				{
					/*凌晨更新策略，不执行最后一次动作*/
				}
			}
			else
			{
				reg.RW.lamp[i] = compareStruct.lastScene.ctrl_buf[i];
			}
		}
#if (PROJECT_TYPEDEF == NB_ONENET || PROJECT_TYPEDEF == NB)
		//device_time.lamp_data_report_time = 120000 + device_info.random;
		device_time.lamp_data_report_time = reg.W.heartbeat_interval * 100 + device_info.random;
#else
		device_time.lamp_data_report_time = 60000 + device_info.random;
#endif
		flag = 1;
	}
	else
	{
		flag = 0;
	}
	if(leakDete.time_on)
	{
		
	}
		
}
/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/
uint32_t timestampTable[SCENE_TIME_NUM];
void scene_deal(uint8_t *pSceneBuf)
{
	volatile uint16_t i = 0, j = 0, hourMin;
	volatile uint32_t timestamp, timestamp_s;
	uint8_t currnetTimeTableSeek;

	reg.W.scene_AllPacketNum = *pSceneBuf++;	 //总包数
	reg.W.scene_CurrentPacketCnt = *pSceneBuf++; //当前的包序号

	if (!reg.W.scene_CurrentPacketCnt) //当前包序号为0
	{
		protocol.subpackage_cnt = 0;
		reg.R.scene_sum = 0;

#if (PROJECT_TYPEDEF == NB_ONENET)
		sceneReceiveFinishFlag = FALSE;
#endif

#if (PROJECT_TYPEDEF == NB_ONENET)
		memset(reg.W.scene, 0, sizeof(reg.W.scene));
		memset(&compareStruct, 0, sizeof(compareStruct));
		memset(timestampTable, 0, sizeof(timestampTable));
#else
		memset(reg.W.scene, 0, sizeof(reg.W.scene));
#endif
	}
#if (PROJECT_TYPEDEF == NB_ONENET)
	if (sceneReceiveFinishFlag)
	{
		return;
	}
#endif
	pSceneBuf++;									//此处指向的数据表示总的时间表数量，所以此处直接跳过，在下面得到当前的时间表数量之后再对其赋值
	reg.W.scene_CurrentTimeTableNum = *pSceneBuf++; //动作总数量

	if (reg.W.scene_CurrentTimeTableNum > SCENE_TIME_NUM)
	{
		LOGD("ERROR:reg.W.scene_CurrentTimeTableNum error:reg.W.scene_CurrentTimeTableNum:%d", reg.W.scene_CurrentTimeTableNum);
		return;
	}

	currnetTimeTableSeek = reg.W.scene_CurrentPacketCnt * 4;
	for (i = 0; i < reg.W.scene_CurrentTimeTableNum; i++)
	{
		memmove((void *)&reg.W.scene[currnetTimeTableSeek + i].scene_id, pSceneBuf, 11);
		pSceneBuf += 11;

		if (reg.W.scene[i].time_ctrl_ch_num > CTRL_LAMP_CH)
		{
			//LOGD("ERROR:reg.W.scene[i].time_ctrl_ch_num error:reg.W.scene[i].time_ctrl_ch_num:%d", reg.W.scene[i].time_ctrl_ch_num);
			return;
		}
		for (j = 0; j < reg.W.scene[i].time_ctrl_ch_num; j++)
		{
			reg.W.scene[currnetTimeTableSeek + i].ctrl_buf[j].sw = *(pSceneBuf++);
			reg.W.scene[currnetTimeTableSeek + i].ctrl_buf[j].light = *(pSceneBuf++);
		}
		reg.W.scene[currnetTimeTableSeek + i].hour = *(pSceneBuf++);
		reg.W.scene[currnetTimeTableSeek + i].min = *(pSceneBuf++);
		reg.W.scene[currnetTimeTableSeek + i].sec = *(pSceneBuf++);
		reg.R.scene_sum++;
	}
	//重新整理时间表
	reg.W.scene_AllTimeTableNum = currnetTimeTableSeek + reg.W.scene_CurrentTimeTableNum; //获取总的时间表
	memset(timestampTable, 0, sizeof(timestampTable));
	for (i = 0; i < reg.W.scene_AllTimeTableNum; i++) //获取对应时间戳
	{

		if (reg.W.scene[i].time_type == 1)
		{
			reg.W.scene[i].time_type = 0xFE;
		}
		//if ((1 != reg.W.scene[i].scene_type) && (6 != reg.W.scene[i].scene_type))
		if (1 != reg.W.scene[i].scene_type)
		{
			hourMin = reg.W.scene[i].sec + (reg.W.scene[i].min << 8);
			GET_NEW_VALUE;
		}
	}

	if (reg.W.scene_AllPacketNum == (reg.W.scene_CurrentPacketCnt + 1)) //当前包是最后一包数据时，对时间表重新排序
	{
#if (PROJECT_TYPEDEF == NB_ONENET)
		sceneReceiveFinishFlag = TRUE;
#endif
		FirstSort = 0;
		sort(timestampTable, reg.W.scene, SCENE_TIME_NUM); //根据时间戳对时间戳和策略时间表重新排序
		// //根据上个时间策略内容执行开关灯
		for (i = 0; i < compareStruct.lastScene.time_ctrl_ch_num; i++)
		{
			{
				reg.RW.lamp[i] = compareStruct.lastScene.ctrl_buf[i];
			}
		}
#if (PROJECT_TYPEDEF == NB_ONENET || PROJECT_TYPEDEF == NB)
		//device_time.lamp_data_report_time = 120000 + device_info.random;
		device_time.lamp_data_report_time = reg.W.heartbeat_interval * 100 + device_info.random;
#else
		device_time.lamp_data_report_time = 60000 + device_info.random;
#endif
	}
}
/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/
struct ota_state
{
	u32 bank_full_flag;
	u16 bank_num;
	u16 bank_offset;
};
extern uint8_t OTA_Result_flag;
#define PAGE_SIZE 128
#define BANK_SIZE 6400

uint8_t OTA_status_before = 0xff;
struct ota_state otaState[2] = {0};
// void ota_deal(uint16_t startAddr, uint8_t *otaBuf)
void ota_deal(uint16_t startAddr, uint8_t *otaBuf, uint8_t len)
{
	u32 bank_num, bank_offset;
	u32 eeprom_bank, eeprom_bank_addr, addr;
	u32 otaDeviceTypedef;
	uint8_t data_len = len;

	if (data_len > FLASH_DATA_BUF)
	{
		//LOGD("ERROR:data_len error:data_len:%d", data_len);
		return;
	}

	if (startAddr == 0x2700)
	{
		memset(otaState, 0, sizeof(otaState));
		reg.W.ota_sum_len = 0;

		//ota_checkBuf = 0x26ff;
		memset(&device_time, 0, sizeof(device_time));
		otaDeviceTypedef = otaBuf[31] << 24 | otaBuf[30] << 16 | otaBuf[29] << 8 | otaBuf[28];
		if (DEVICE_TYPEDEF == otaDeviceTypedef)
		{
			LOGD("OK:file \r\n");
			ota_before_ready();
		}
		else{
			LOGD("ERROR:file \r\n");
			//reg.RW.ota_flag = OTA_FAIL;
#if (PROJECT_TYPEDEF == NB)
			device_info.mqtt_state = NB_LWM2M_OK;
			
			OTA_Result_flag = 0x08;

			//my_queue_init();
			memset(main_tx.my_at_sendbuf, 0, sizeof(main_tx.my_at_sendbuf));
			memset(main_tx.my_at_data_str, 0, sizeof(main_tx.my_at_data_str));
			
			//my_cmd_deal(DOWMLINK_READ, APP_PACKsend_report_ota_status_22, waiting_to_receive, 50, 2000, NO_ACK);
			mainUart.my_data_buf[3] = 22;
			APP_PACKsend_report_ota_status_22();
			my_usart_send_data(main_tx.my_at_sendbuf, strlen((char *)main_tx.my_at_sendbuf));
#else
			OTA_status_before = reg.RW.ota_flag;
			reg.RW.ota_flag = OTA_FILE_ERROR;
#endif
			return;
		}	
	}

	bank_offset = (startAddr - 0x2700) % 32;
	bank_num = (startAddr - 0x2700) / 32;

	if (bank_num != otaState[bank_num % 2].bank_num)
	{
		if (otaState[bank_num % 2].bank_full_flag != 0)
		{
			//LOGD("error:bank_full_flag:%4x", otaState[bank_num % 2].bank_full_flag); //升级失败
		}
		else
		{
			otaState[bank_num % 2].bank_num = bank_num;
			if (bank_num % 2)
			{
				eeprom_earse(EEPROM1_IAP_ADDR, BANK_SIZE);
			}
			else
			{
				eeprom_earse(EEPROM0_IAP_ADDR, BANK_SIZE);
			}
		}
	}
	memmove(flash_data, otaBuf, data_len * 2);
	addr = (startAddr - 0x2700) * 200;

	eeprom_bank = (addr / BANK_SIZE) % 2;
	eeprom_bank_addr = addr % BANK_SIZE;
	if (eeprom_bank == 0)
	{
		eeprom_program(EEPROM0_IAP_ADDR + eeprom_bank_addr, (u32)flash_data, data_len * 2);
	}
	else
	{
		eeprom_program(EEPROM1_IAP_ADDR + eeprom_bank_addr, (u32)flash_data, data_len * 2);
	}
	otaState[bank_num % 2].bank_full_flag |= 1 << bank_offset;
	otaState_check(0);
	reg.W.ota_sum_len += data_len * 2;
	LOGD("OTA LEN:%d\r\n", reg.W.ota_sum_len);
}

/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/
void otaState_check(uint8_t rx_ok_flag)
{
	u32 i;
	if (rx_ok_flag)
	{
		if (otaState[0].bank_full_flag)
		{
			otaState[0].bank_full_flag = 0xffffffff;
		}
		else
		{
			otaState[1].bank_full_flag = 0xffffffff;
		}
	}
	if (otaState[0].bank_full_flag == 0xffffffff) //所有填充满,满6400字节
	{
		otaState[0].bank_full_flag = 0;
		for (i = 0; i < (BANK_SIZE / PAGE_SIZE); i++)
		{
			WDT->EN = 0xbb;
			FlashReadALL(EEPROM0_IAP_ADDR + (i * PAGE_SIZE), flash_data, PAGE_SIZE / 2);
			flash_program(FLASH_OTA_ADDR + (i * PAGE_SIZE) + otaState[0].bank_num * BANK_SIZE, (u32)flash_data, PAGE_SIZE);
		}
	}
	if (otaState[1].bank_full_flag == 0xffffffff)
	{
		otaState[1].bank_full_flag = 0;
		for (i = 0; i < (BANK_SIZE / PAGE_SIZE); i++)
		{
			WDT->EN = 0xbb;
			FlashReadALL(EEPROM1_IAP_ADDR + (i * PAGE_SIZE), flash_data, PAGE_SIZE / 2);
			flash_program(FLASH_OTA_ADDR + (i * PAGE_SIZE) + otaState[1].bank_num * BANK_SIZE, (u32)flash_data, PAGE_SIZE);
		}
	}
}
/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/

void ota_check(void)
{

	static u8 count = 0;
#if (PROJECT_TYPEDEF == NB_ONENET)
	static uint16_t OTA_Reset_delay = 0;
	if (reg.RW.ota_flag == OTA_START)
	{
		OTA_Reset_delay++;
		if (OTA_Reset_delay >= 100 * 5)
		{
			OTA_Reset_delay = 0;
			reg.RW.ota_flag = NONE_OTA;
			data_flash_write();
			IAP_APP_Jump();
		}
	}
	else
	{

		OTA_Reset_delay = 0;
	}
#endif
	if (reg.RW.ota_flag != OTA_READY)
		return;
#if (PROJECT_TYPEDEF == NB_ONENET)
	if ((++device_time.ota_timeout) >= 100 * 30) //五分钟
#elif (PROJECT_TYPEDEF == CAT1)
	if ((++device_time.ota_timeout) >= 100 * 60 * 2) //五分钟
#elif (PROJECT_TYPEDEF == NB)
	if ((++device_time.ota_timeout) >= 100 * 60 * 2) //五分钟
#endif
	{
		device_time.ota_timeout = 0;
#if (PROJECT_TYPEDEF == NB_ONENET)
		if ((++count) >= 5)
#elif (PROJECT_TYPEDEF == CAT1)
		if ((++count) >= 2)
#elif (PROJECT_TYPEDEF == NB)
		if ((++count) >= 5)
#endif
		{
#if (PROJECT_TYPEDEF == NB)
			count = 0;
			reg.RW.ota_flag = OTA_FAIL;
			device_info.mqtt_state = NB_LWM2M_OK;
			OTA_Result_flag = 0x06;
			my_cmd_deal(DOWMLINK_READ, APP_PACKsend_report_ota_status_22, waiting_to_receive, 1, 2000, ACK);
#else
			count = 0;
			reg.RW.ota_flag = OTA_FAIL;
			protocol.reg_num = 2;
			protocol.reg_addr[0] = 0x1008;
			protocol.reg_len[0] = 1;
			protocol.reg_addr[1] = 0x1007;
			protocol.reg_len[1] = 1;
#if (PROJECT_TYPEDEF == CAT1)
			my_cmd_deal(AT_QMTPUB_UPLINK, AT_TX_UPLINK10, AT_RX_UPLINK, 1, 1000, ACK);
			LOGD("test02:AT_TX_UPLINK10\r\n");
#endif
			my_cmd_deal(AT_QMTPUB_UP_LAMP, AT_TX_QMTPUB_UP_LAMP, waiting_to_receive, 1, 2000, ACK);
#if (PROJECT_TYPEDEF == NB_ONENET)
			device_info.mqtt_state = NB_LWM2M_OK;
#endif
#endif
		}
		else
		{
#if (PROJECT_TYPEDEF == NB)

			my_cmd_deal(DOWMLINK_READ, APP_PACKsend_request_ota_pack_21, waiting_to_receive, 1, 2000, ACK);
#else
			if (protocol.reg_addr[0] == 0x2018)
			{
				protocol.reg_addr[0] = 0x1007;
				protocol.data[0] = protocol_read(0x1007);
			}
#if (PROJECT_TYPEDEF == CAT1)
			my_cmd_deal(AT_QMTPUB_UPLINK, AT_TX_UPLINK06, waiting_to_receive, 1, 1000, ACK);
#endif
			my_cmd_deal(DOWMLINK_READ, fun_code_singleWrite, waiting_to_receive, 10, 2000, NO_ACK);

#endif
		}
	}
}
/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/
u16 ota_checkBuf = 0x26ff;
void protocol_write(uint16_t start_addr, uint8_t *data, uint16_t len)
{
	volatile uint16_t i = 0, bank = 0, offset = 0;
	volatile uint16_t *p = NULL;

	/* 寄存器地址右移，取低两位 */
	bank = start_addr / 4096; //计算分区
	/* 策略包 */
	if (start_addr == 0x201E)
	{
		scene_deal(data);
	}
	else if (start_addr >= 0x2700 && start_addr < 0x3000) //固件升级
	{
		if ((ota_checkBuf + 1) == start_addr) //防重包
		{
			ota_checkBuf = start_addr;
			ota_deal(start_addr, data, len);
		}
	}
	else if(start_addr == 0x101B)
	{
		reg.RW.cpmpare_delta = (reg.RW.cpmpare_delta & 0xFF00) | data[1];
		reg.W.lamp_cpmpare_z = (reg.W.lamp_cpmpare_z & 0xFF00) | data[1];
		data_flash_write();
	}
	else
	{
		switch (bank)
		{
		case 1: //读写
			offset = start_addr - 0x1003;
			p = (uint16_t *)&reg.RW;
			if (offset * 2 >= protocol_size.rw)
			{
				//LOGD("ERROR:write offset error:offset:%d,protocol_size.rw:%d", offset * 2, protocol_size.rw);
				return;
			}
			break;
		case 2: //只写
			offset = start_addr - 0x2001;
			p = (uint16_t *)&reg.W;
			if (offset * 2 >= protocol_size.w)
			{
				//LOGD("ERROR:write offset error:offset:%d,protocol_size.w:%d", offset * 2, protocol_size.w);
				return;
			}
			break;
		default:
			return;
		}

		for (i = 0; i < len; i++)
		{
			*(p + offset + i) = pCharToU16(data);
			data += 2;
		}
	}
}

/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/
uint16_t protocol_read(uint16_t start_addr)
{
	volatile uint16_t i, bank, offset;
	uint16_t *p = NULL;
	bank = start_addr / 4096;
	switch (bank)
	{
	case 1: //读写
		offset = start_addr - 0x1003;
		p = (uint16_t *)&reg.RW;
		if (offset * 2 < protocol_size.rw) //防止越界
		{
			return *(p + offset);
		}
		else
		{
			//LOGD("ERROR:read offset error:offset:%d,protocol_size.rw:%d", offset * 2, protocol_size.rw);
		}
		break;

	case 2:
		offset = start_addr - 0x2001;
		p = (uint16_t *)&reg.W;
		if (offset * 2 < protocol_size.w) //防止越界
		{
			return *(p + offset);
		}
		else
		{
			//LOGD("ERROR:read offset error:offset:%d,protocol_size.w:%d", offset * 2, protocol_size.w);
		}
		break;

	case 3:
		if(start_addr < 0x3200)
		{
			offset = start_addr - 0x3001;
			p = (uint16_t *)&reg.R;
			if (offset * 2 < protocol_size.r) //防止越界
			{
				return *(p + offset);
			}
			else
			{
				//LOGD("ERROR:read offset error:offset:%d,protocol_size.r:%d", offset * 2, protocol_size.r);
			}
		}
		else
		{
			offset = start_addr - 0x3200;
			p = (uint16_t *)&ALARM_R;
			if (offset * 2 < sizeof(ALARM_R)) //防止越界
			{
				return *(p + offset);
			}
			else
			{
				//LOGD("ERROR:read offset error:offset:%d,protocol_size.r:%d", offset * 2, protocol_size.r);
			}
		}
		break;

	default:
		break;
	}
	return 0;
}

/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/
// void check_sw_lamp(uint8_t sw, uint8_t sw_status,uint32_t timestamp,uint32_t *power_timestamp,uint32 *currut_lamp1_on_times)
// {
// 	if(*power_timestamp == 0 && sw == sw_status)
// 	{
// 		*power_timestamp = timestamp;
// 	}

// 	if(sw == 1 && *power_timestamp)
// 	{
// 		*currut_lamp1_on_times = timestamp;
// 	}
// }



void check_lamp_sw(uint8_t sw1,uint8_t sw2,  uint32_t timestamp)
{
	if(timestamp < 1681356681 || leakDete.time_on == 0 || reg.RW.ota_flag)
		return;
	
	
	if(reg.W.lamp1_power_on_timestamp == 0 && leakDete.currut_lamp1_on_timestamp)
	{
		reg.W.lamp1_power_on_timestamp = leakDete.currut_lamp1_on_timestamp;
		leakDete.currut_lamp1_on_timestamp = 0;
	}
	if(reg.W.lamp2_power_on_timestamp == 0 && leakDete.currut_lamp2_on_timestamp)
	{
		reg.W.lamp2_power_on_timestamp = leakDete.currut_lamp2_on_timestamp;
		leakDete.currut_lamp2_on_timestamp = 0;
	}
	
	if(sw1)
	{
		if(reg.W.lamp1_power_on_timestamp == 0)
		{
			reg.W.lamp1_power_on_timestamp = timestamp;
		}
		else if(reg.W.lamp1_power_on_timestamp  && leakDete.currut_lamp1_on_timestamp == 0 && leakDete.send_lamp1_time_flag == 1)
		{
			leakDete.currut_lamp1_on_timestamp = timestamp;
		}
		if(reg.W.lamp1_power_on_timestamp == 0 && leakDete.currut_lamp1_on_timestamp)
		{
			reg.W.lamp1_power_on_timestamp = leakDete.currut_lamp1_on_timestamp;
			LOGD("\r\n lamp1_power_on_timestamp time:%u\r\n", leakDete.system_run_timestamp);
			leakDete.currut_lamp1_on_timestamp = 0;
		}
	}
	else
	{
		if(reg.W.lamp1_power_off_timestamp == 0 && reg.W.lamp1_power_on_timestamp)
		{
			reg.W.lamp1_power_off_timestamp = timestamp;
		}
	}
	
	if(sw2)
	{
		if(reg.W.lamp2_power_on_timestamp == 0)
		{
			reg.W.lamp2_power_on_timestamp = timestamp;
		}
		else if(reg.W.lamp2_power_on_timestamp  && leakDete.currut_lamp2_on_timestamp == 0 && leakDete.send_lamp2_time_flag == 1)
		{
			leakDete.currut_lamp1_on_timestamp = timestamp;
		}
		
	}
	else
	{
		if(reg.W.lamp2_power_off_timestamp == 0 && reg.W.lamp2_power_on_timestamp)
		{
			reg.W.lamp2_power_off_timestamp = timestamp;
		}
	}

	

	if(reg.W.lamp1_power_off_timestamp && reg.W.lamp1_power_off_timestamp && leakDete.send_lamp1_time_flag == 0)
	{
		leakDete.send_lamp1_time_flag = 1;
	}
	if(reg.W.lamp2_power_off_timestamp && reg.W.lamp2_power_off_timestamp && leakDete.send_lamp2_time_flag == 0)
	{
		leakDete.send_lamp2_time_flag = 1;

	}

	if(leakDete.send_lamp1_time_flag == 1 || leakDete.send_lamp2_time_flag == 1)
	{
		if(++leakDete.delay_send_time < device_info.random)
			return;

		if(leakDete.send_lamp1_time_flag == 1 && leakDete.send_lamp2_time_flag == 0)
		{
			leakDete.delay_send_time = 0;
			protocol.reg_num = 1;
			protocol.reg_addr[0] = 0x206C;
			protocol.reg_len[0] = 4;
			leakDete.send_lamp1_time_flag = 2;
#if (PROJECT_TYPEDEF == CAT1)
			my_cmd_deal(AT_QMTPUB_UPLINK, AT_TX_UPLINK10, AT_RX_UPLINK, 1, 1000, ACK);
			LOGD("test03:AT_TX_UPLINK10\r\n");
#endif
			my_cmd_deal(AT_QMTPUB_UP_LAMP, AT_TX_QMTPUB_UP_LAMP, waiting_to_receive, 1, 2000, ACK);
		}
		else if(leakDete.send_lamp1_time_flag == 0 && leakDete.send_lamp2_time_flag == 1)
		{
			leakDete.delay_send_time = 0;
//			protocol.reg_num = 1;
//			protocol.reg_addr[0] = 0x2070;
//			protocol.reg_len[0] = 4;
//			leakDete.send_lamp1_time_flag = 2;
//#if (PROJECT_TYPEDEF == CAT1)
//			my_cmd_deal(AT_QMTPUB_UPLINK, AT_TX_UPLINK10, AT_RX_UPLINK, 1, 1000, ACK);
//#endif
//			my_cmd_deal(AT_QMTPUB_UP_LAMP, AT_TX_QMTPUB_UP_LAMP, waiting_to_receive, 1, 2000, ACK);
		}
		else if(leakDete.send_lamp1_time_flag == 1 && leakDete.send_lamp2_time_flag == 1)
		{
			leakDete.delay_send_time = 0;
			protocol.reg_num = 1;
			protocol.reg_addr[0] = 0x206C;
			protocol.reg_len[0] = 8;
			leakDete.send_lamp1_time_flag = 2;
#if (PROJECT_TYPEDEF == CAT1)
			my_cmd_deal(AT_QMTPUB_UPLINK, AT_TX_UPLINK10, AT_RX_UPLINK, 1, 1000, ACK);
			LOGD("test03:AT_TX_UPLINK10\r\n");
#endif
			my_cmd_deal(AT_QMTPUB_UP_LAMP, AT_TX_QMTPUB_UP_LAMP, waiting_to_receive, 1, 2000, ACK);
		}
	}
}


/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/

/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/
